<script setup name="BillCurd">
import {billAdd, billDel, billEdit, billGetTypeList} from "@/api/fms/bill";
import {accountGetList} from "@/api/fms/account";

import OrderSearch from "@/views/wms/order/search";
import {getCurrentInstance, reactive} from "vue";

const {proxy} = getCurrentInstance();
const {hrp_user_sex} = proxy.useDict('hrp_user_sex');
let pd = reactive({
  accountList: [],
  open: false,
  title: "",
  operation: 0,
  form: {},
  billType: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null
  },
  rules: {
    enableFlag: [{
      required: true,
      message: "是否不能为空",
      trigger: "change"
    }],
    name: [{
      required: true,
      message: "名称不能为空",
      trigger: "blur"
    }]
  }
});

function initCurd(){
  accountGetList({enableFlag: "0"}).then(res => {
    pd.accountList = res.data;
    pd.form.accountId = pd.accountList[0].id;
  });
  billGetTypeList({typeId: pd.form.typeId}).then(res => {
    pd.billType = res.data[0];
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["billRef"].validate(valid => {
    if (valid) {
      if (pd.operation == 1) {
        billAdd(pd.form).then(res => {
          proxy.$modal.msgSuccess("新增成功");
        });
      } else if (pd.operation == 2) {
        billEdit(pd.form).then(response => {
          proxy.$modal.msgSuccess("修改成功");
        });
      }
      pd.open = false;
      emit("ok", pd.operation);
    }
  });
}

// 新增
function add(bill) {
  reset();
  //合并
  pd.form = {...pd.form, ...bill};
  pd.title = "新增财务单据";
  pd.operation = 1;
  pd.open = true;
  initCurd();
}

// update
function edit(bill) {
  reset();
  //合并
  pd.form = {...pd.form, ...bill};
  pd.title = "修改财务单据";
  pd.operation = 2;
  pd.open = true;
  initCurd();
}

// del
function del(id) {
  pd.operation = 3;
  proxy.$modal.confirm('是否确认删除财务单据编号为:"' + id + '"的数据项？').then(function () {
    return billDel(id);
  }).then(() => {
    proxy.$modal.msgSuccess("删除成功");
    pd.open = false;
    emit("ok", pd.operation);
  }).catch(() => {
  });
}

// 表单重置
function reset() {
  pd.form = {
    id: null,
    name: null,
    costMoney:null,
    classificationItemId: null,
    userId: null,
    state: "5",
    remark: null
  };
  pd.operation = 0;
  pd.title = "";
  proxy.resetForm("billRef");
}

defineExpose({add, edit, del});
const emit = defineEmits(["ok"]);

function orderSelected(list){
  list.forEach(item => {
    pd.form.name = item.name;
    pd.form.sourceId = item.id;
    pd.form.userId = item.userId;
    pd.form.costMoney = item.costMoney;

  })
}

</script>

<template>
  <!-- 添加或修改财务单据对话框 -->
  <el-dialog :title="pd.title" v-model="pd.open" width="700px" append-to-body>
    <el-form ref="billRef" :model="pd.form" :rules="pd.rules" label-width="80px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="单据名称" prop="name">
            <el-input v-model="pd.form.name" placeholder="请输入单据名称"/>
          </el-form-item>
          <el-form-item label="来源单号" prop="name">
            <OrderSearch :dataText="pd.form.sourceId" :searchType=1 :params="{state: '3'}" @ok="orderSelected" />
          </el-form-item>
          <el-form-item :label="pd.billType.costMoney" prop="costMoney">
            <el-input :style="{width: '150px'}" v-model="pd.form.costMoney" placeholder="请输入应收金额"/>
          </el-form-item>
          <el-form-item :label="pd.billType.planTime" prop="planTime">
            <el-date-picker clearable :style="{width: '150px'}" v-model="pd.form.planTime"
                            type="date" value-format="YYYY-MM-DD" placeholder="请选择时间">
            </el-date-picker>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="用户方" prop="name">
            <el-input v-model="pd.form.userId" placeholder="请输入userId"/>
          </el-form-item>
          <el-form-item label="财务账户" prop="name">
            <el-select :style="{width: '150px'}" v-model="pd.form.accountId" placeholder="请选择账户" clearable>
              <el-option v-for="item in pd.accountList" :key="item.id" :label="item.name" :value="item.id"/>
            </el-select>
          </el-form-item>
          <el-form-item :label="pd.billType.money" prop="money">
            <el-input :style="{width: '150px'}" v-model="pd.form.money" placeholder="请输入实收金额"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="财务项目" prop="name">
            <el-input v-model="pd.form.classificationItemId" placeholder="请输入财务项目"/>
          </el-form-item>
          <el-form-item label="状态" prop="state">
            <el-switch v-model="pd.form.state" active-value="0" inactive-value="1"></el-switch>
          </el-form-item>
          <el-form-item label="操作人" prop="name">
            <el-input v-model="pd.form.operId" placeholder="请输入来源号"/>
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="备注" prop="remark">
        <el-input v-model="pd.form.remark" type="textarea" placeholder="请输入内容"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button v-if="pd.operation===1" type="primary" @click="submitForm">新 增</el-button>
        <el-button v-if="pd.operation===2" type="success" @click="submitForm">修 改</el-button>
        <el-button v-if="pd.operation===2" type="danger" @click="del(pd.form.id)">删 除</el-button>
        <el-button type="info" @click="pd.open = false">关 闭</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style lang='scss' scoped>

</style>